




var _oMessages =
{
sCloseAlert:	LOCID_FORMS_SAVE_CONFIRM_TITLE

};






var _oConst =
{

text:	"text",
$none:	"$none",
none:	"none",
block:	"block",


sInvalidSchemaNameChars:		"[^A-Za-z0-9_]",
sAttributeSchemaNameFormat:		"{0}Id",
sRelationshipEditorUrlFormat:	"manageRelationship.aspx?entityRole={0}&entityRelationshipId={1}&entityId={2}",
sInformationPageId: 			"divInformation",
sMappingsPageId: 				"divMappings"
};






var _bSaving = false;
var _sInputXml = "";










function ManageRelationshipJsWindowOnLoad()
{
if (!_bCreate)
{
window.focus();
}

initControls();

_sInputXml = getRelationshipXml();

loadPage(_oConst.sInformationPageId);


try
{
if (_bManyToMany)
{

if (selCEDisplayOption != null && !selCEDisplayOption.Disabled && selCEDisplayOption.style.display != "none")
{
selCEDisplayOption.SetFocus();
}
else if (selCEName != null && !selCEName.Disabled && selCEName.style.display != "none")
{
selCEName.SetFocus();
}
else if (selOEDisplayOption != null && !selOEDisplayOption.Disabled && selOEDisplayOption.style.display != "none")
{
selOEDisplayOption.SetFocus();
}
else if	(txtSchemaName != null && !txtSchemaName.Disabled && txtSchemaName.style.display != "none")
{
txtSchemaName.SetFocus();
}
else if (txtIntersectEntityName != null && !txtIntersectEntityName.Disabled && txtIntersectEntityName.style.display != "none")
{
txtIntersectEntityName.SetFocus();
}
}
else
{
if (selReferencedEntityName != null && !selReferencedEntityName.Disabled && selReferencedEntityName.style.display != "none")
{
selReferencedEntityName.SetFocus();
}
else if (selReferencingEntityName != null && !selReferencingEntityName.Disabled && selReferencingEntityName.style.display != "none")
{
selReferencingEntityName.SetFocus();
}
else if	(txtSchemaName != null && !txtSchemaName.Disabled && txtSchemaName.style.display != "none")
{
txtSchemaName.SetFocus();
}
else if (txtReferencedInstanceName != null && !txtReferencedInstanceName.Disabled && txtReferencedInstanceName.style.display != "none")
{
txtReferencedInstanceName.SetFocus();
}
else if (txtAttributeSchemaName != null && !txtAttributeSchemaName.Disabled && txtAttributeSchemaName.style.display != "none")
{
txtAttributeSchemaName.SetFocus();
}
else if (selAttributeType != null && !selAttributeType.Disabled && selAttributeType.style.display != "none")
{
selAttributeType.SetFocus();
}
else if (selReqLevel != null && !selReqLevel.Disabled && selReqLevel.style.display != "none")
{
selReqLevel.SetFocus();
}
else if (selDisplayOption != null && !selDisplayOption.Disabled && selDisplayOption.style.display != "none")
{
selDisplayOption.SetFocus();
}
else if (selType != null && !selType.Disabled && selType.style.display != "none")
{
selType.SetFocus();
}
}
}
catch(e)
{}
}

window.attachEvent('onload', ManageRelationshipJsWindowOnLoad);




window.attachEvent("onbeforeunload", window_onbeforeunload);
function window_onbeforeunload()
{
if (!_bSaving && isFormDirty())
{
event.returnValue = _oMessages.sCloseAlert;
}
}




function initControls()
{

_bSaveCustomCascadeParams = (selType.DataValue == _oCascadeTypes.sCustom);
onEntityChange();
}





function document.onkeydown()
{
return ProcessKeyDown();
}

function _save()
{
saveRelationshipAction(false);
}

function _saveandclose()
{
saveRelationshipAction(true);
}

function _saveandnew()
{
}










function CallbackParams(bClose, bReload, bRefreshGrid)
{
this.bClose			= bClose;
this.bReload		= bReload;
this.bRefreshGrid	= bRefreshGrid;
}

function remoteRelationshipCommandCallback(oResult, oCallbackParams)
{
if (oResult.Success)
{

if (oResult.RemoteCommand.Command == _oRelUtlConst.sCreateRelationshipCommand)
{
_sEntityRelationshipId = oResult.ReturnValue;
}
else if (oResult.RemoteCommand.Command == _oRelUtlConst.sDeleteRelationshipCommand)
{
if (!IsNull(oResult.ReturnValue) && oResult.ReturnValue.Used)
{
showUsage(_oSCUConst.sRelationshipMode, oResult.ReturnValue);
return true;
}
}


if (oCallbackParams.bRefreshGrid)
{
try
{
window.opener.gridRelationships.Refresh();
}
catch(e)
{}
}


if (oCallbackParams.bClose)
{
window.close();
return false;
}


if (oCallbackParams.bReload)
{

frmReload.action = formatString(_oConst.sRelationshipEditorUrlFormat, _sEntityRole, _sEntityRelationshipId, _sEntityId);
frmReload.submit();
return false;
}
}

_bSaving = false;
return true;
}




function saveRelationshipAction(bClose)
{
if (!_bSaving && validateParameters())
{
_bSaving = true;

var sDataXml = getRelationshipXml();
var oCallbackParams = new CallbackParams(bClose, true, true);

if (_bCreate)
{
createRelationship(sDataXml, remoteRelationshipCommandCallback, oCallbackParams);
}
else if (isFormDirty())
{
updateRelationship(sDataXml, remoteRelationshipCommandCallback, oCallbackParams);
}
else if (bClose)
{
window.close();
return;
}
else
{
_bSaving = false;
}
}
}




function deleteRelationshipAction()
{
var oCallbackParams = new CallbackParams(true, false, true);
deleteRelationship(_sEntityRelationshipId, true, remoteRelationshipCommandCallback, oCallbackParams);
}




var _oVisiblePage = null;
function loadPage(sPageId)
{
var oPage = document.all(sPageId);


if (_oVisiblePage != null)
{
_oVisiblePage.style.display = "none";
}


_oVisiblePage = oPage;
_oVisiblePage.style.display = "inline";


if (sPageId != _oConst.sInformationPageId)
{
var oIFrameElement	= oPage.children(0);
var oIFrame			= document.frames(oIFrameElement.id);
if (oIFrame.location.href.indexOf("/_root/Blank.aspx") > -1)
{
oIFrame.location = oIFrameElement.url;
}
}
}











function getRelationshipXml()
{
var oDataXml = createXmlDoc(_oTags.relationship);


addTextXmlNode(oDataXml, _oTags.relationshipid, _sRelationshipId);


addTextXmlNode(oDataXml, _oTags.entityrelationshipid, _sEntityRelationshipId);



var schemaName = txtSchemaName.value;
if (window.txtPrefix)
{
schemaName = txtPrefix.innerText + txtSchemaName.value;
}
addTextXmlNode(oDataXml, _oTags.schemaname, schemaName);


if (_bManyToMany)
{

addTextXmlNode(oDataXml, _oTags.manytomany, "true");



var intersectEntityName = txtIntersectEntityName.value;
if (window.txtPrefix2)
{
intersectEntityName = txtPrefix2.innerText + txtIntersectEntityName.value;
}
addTextXmlNode(oDataXml, _oTags.intersectentityname, intersectEntityName);


addTextXmlNode(oDataXml, _oTags.currententityname, selCEName.DataValue);


{
var oAssociatedMenuXml = addXmlNode(oDataXml, _oTags.associatedmenu);

addControlDataValue(oAssociatedMenuXml, _oTags.displayoption,	selCEDisplayOption);
addControlDataValue(oAssociatedMenuXml, _oTags.displayarea,		selCEDisplayArea);
addControlDataValue(oAssociatedMenuXml, _oTags.navpaneorder,	txtCEDisplayOrder);
addControlDataValue(oAssociatedMenuXml, _oTags.customlabel,		txtCECustomLabel);
}


addTextXmlNode(oDataXml, _oTags.otherentityname, selOEName.DataValue);


{
var oAssociatedMenuXml = addXmlNode(oDataXml, _oTags.associatedmenuotherentity);

addControlDataValue(oAssociatedMenuXml, _oTags.displayoption,	selOEDisplayOption);
addControlDataValue(oAssociatedMenuXml, _oTags.displayarea,		selOEDisplayArea);
addControlDataValue(oAssociatedMenuXml, _oTags.navpaneorder,	txtOEDisplayOrder);
addControlDataValue(oAssociatedMenuXml, _oTags.customlabel,		txtOECustomLabel);
}
}
else
{

addTextXmlNode(oDataXml, _oTags.manytomany, "false");


addTextXmlNode(oDataXml, _oTags.referencedentityname, selReferencedEntityName.DataValue);
addControlDataValue(oDataXml, _oTags.referencedinstancename, txtReferencedInstanceName);


addTextXmlNode(oDataXml, _oTags.referencingentityname, selReferencingEntityName.DataValue);


addControlDataValue(oDataXml, _oTags.reqlevel, selReqLevel);
addControlDataValue(oDataXml, _oTags.attributeschemaname, txtAttributeSchemaName);



if (!selType.Disabled)
{
var oTypeXml = addXmlNode(oDataXml, _oTags.cascading);


addControlDataValue(oTypeXml, _oTags.assign,	selCascadeAssign,	true);
addControlDataValue(oTypeXml, _oTags.share,		selCascadeShare,	true);
addControlDataValue(oTypeXml, _oTags.unshare,	selCascadeUnshare,	true);
addControlDataValue(oTypeXml, _oTags.reparent,	selCascadeReparent,	true);




if (_bCustom == true)
{
addControlDataValue(oTypeXml, _oTags.Delete,	selCascadeDelete,	true);
}
}


addControlDataValue(oDataXml, _oTags.description, txtDescription);


{
var oAssociatedMenuXml = addXmlNode(oDataXml, _oTags.associatedmenu);

addControlDataValue(oAssociatedMenuXml, _oTags.displayoption,	selDisplayOption);
addControlDataValue(oAssociatedMenuXml, _oTags.displayarea,		selDisplayArea);
addControlDataValue(oAssociatedMenuXml, _oTags.navpaneorder,	txtDisplayOrder);
addControlDataValue(oAssociatedMenuXml, _oTags.customlabel,		txtCustomLabel);
}
}
return convertXmlDocToString(oDataXml);
}











function validateParameters()
{
var bValid = true;


bValid = bValid && validateSchemaName(txtSchemaName);

if (_bManyToMany)
{

bValid = bValid && validateSchemaName(txtIntersectEntityName);


bValid = bValid && validateRequiredValue(txtCECustomLabel);


bValid = bValid && validateRequiredValue(selOEName);


bValid = bValid && validateRequiredValue(txtOECustomLabel);

}
else
{

bValid = bValid && validateRequiredValue(selReferencedEntityName);


bValid = bValid && validateRequiredValue(selReferencingEntityName);


bValid = bValid && validateRequiredValue(txtReferencedInstanceName);


bValid = bValid && validateRequiredValue(selReqLevel);


bValid = bValid && validateSchemaName(txtAttributeSchemaName);


bValid = bValid && validateRequiredValue(selType);


bValid = bValid && validateRequiredValue(txtCustomLabel);
}

return bValid;
}











function onReferencedInstanceNameChange()
{
if (_bCreate &&
(txtReferencedInstanceName.DataValue != null && txtReferencedInstanceName.DataValue.length > 0) &&
(txtAttributeSchemaName.DataValue == null || txtAttributeSchemaName.DataValue.length == 0))
{
var iNameMaxLen = txtAttributeSchemaName.maxLength - formatString(_oConst.sAttributeSchemaNameFormat, "").length;
var regExp = new RegExp(_oConst.sInvalidSchemaNameChars, "g");
var sName = txtReferencedInstanceName.DataValue.replace(regExp, "").substr(0, iNameMaxLen);
if (sName.length > 0)
{
txtAttributeSchemaName.DataValue = formatString(_oConst.sAttributeSchemaNameFormat, sName);
}
}
}




function onEntityChange()
{
onTypeChange();
createSchemaName(selReferencedEntityName.DataValue, selReferencingEntityName.DataValue);

}
function onOtherEntityChange()
{

createSchemaName(selCEName.DataValue, selOEName.DataValue);
createIntersectEntityName();
}




var _bSaveCustomCascadeParams = false;
var _oCustomCascadeParams =
{
sAssign:	_oCascadeLinkTypes.sCascade,
sShare:		_oCascadeLinkTypes.sCascade,
sUnshare:	_oCascadeLinkTypes.sCascade,
sReparent:	_oCascadeLinkTypes.sCascade,
sMerge:		_oCascadeLinkTypes.sCascade,
sDelete:	_oCascadeLinkTypes.sCascade
}

function onTypeChange()
{
var sReferencedEntityName	= (selReferencedEntityName.DataValue == null) ?
_oConst.$none : selReferencedEntityName.DataValue;


if (_bSaveCustomCascadeParams)
{
_oCustomCascadeParams.sAssign	= selCascadeAssign.DataValue;
_oCustomCascadeParams.sShare	= selCascadeShare.DataValue;
_oCustomCascadeParams.sUnshare	= selCascadeUnshare.DataValue;
_oCustomCascadeParams.sReparent	= selCascadeReparent.DataValue;
_oCustomCascadeParams.sMerge	= selCascadeMerge.DataValue;
_oCustomCascadeParams.sDelete	= selCascadeDelete.DataValue;
_bSaveCustomCascadeParams = false;
}


var oCascadeParams = null;
switch(selType.DataValue)
{
case _oCascadeTypes.sParental:
{
selCascadeAssign.DataValue		= _oCascadeLinkTypes.sCascade;
selCascadeShare.DataValue		= _oCascadeLinkTypes.sCascade;
selCascadeUnshare.DataValue		= _oCascadeLinkTypes.sCascade;
selCascadeReparent.DataValue	= _oCascadeLinkTypes.sCascade;
selCascadeMerge.DataValue		= typeof(_oMergeableEntities[sReferencedEntityName]) != "undefined" ?
_oCascadeLinkTypes.sCascade : _oCascadeLinkTypes.sNoCascade;
if (_bCustom)
selCascadeDelete.DataValue		= _oCascadeLinkTypes.sCascade;
break;
}
case _oCascadeTypes.sReferential:
{
selCascadeAssign.DataValue		= _oCascadeLinkTypes.sNoCascade;
selCascadeShare.DataValue		= _oCascadeLinkTypes.sNoCascade;
selCascadeUnshare.DataValue		= _oCascadeLinkTypes.sNoCascade;
selCascadeReparent.DataValue	= _oCascadeLinkTypes.sNoCascade;
selCascadeMerge.DataValue		= typeof(_oMergeableEntities[sReferencedEntityName]) != "undefined" ?
_oCascadeLinkTypes.sCascade : _oCascadeLinkTypes.sNoCascade;
selCascadeDelete.DataValue		= _oCascadeLinkTypes.sRemoveLink;
break;
}
case _oCascadeTypes.sRestricted:
{
selCascadeAssign.DataValue		= _oCascadeLinkTypes.sNoCascade;
selCascadeShare.DataValue		= _oCascadeLinkTypes.sNoCascade;
selCascadeUnshare.DataValue		= _oCascadeLinkTypes.sNoCascade;
selCascadeReparent.DataValue	= _oCascadeLinkTypes.sNoCascade;
selCascadeMerge.DataValue		= typeof(_oMergeableEntities[sReferencedEntityName]) != "undefined" ?
_oCascadeLinkTypes.sCascade : _oCascadeLinkTypes.sNoCascade;
selCascadeDelete.DataValue		= _oCascadeLinkTypes.sRestrict;
break;
}
case _oCascadeTypes.sCustom:
{
selCascadeAssign.DataValue		= _oCustomCascadeParams.sAssign;
selCascadeShare.DataValue		= _oCustomCascadeParams.sShare;
selCascadeUnshare.DataValue		= _oCustomCascadeParams.sUnshare;
selCascadeReparent.DataValue	= _oCustomCascadeParams.sReparent;
selCascadeMerge.DataValue		= typeof(_oMergeableEntities[sReferencedEntityName]) != "undefined" ?
_oCascadeLinkTypes.sCascade : _oCascadeLinkTypes.sNoCascade;
if (_bCustom)
selCascadeDelete.DataValue		= _oCustomCascadeParams.sDelete;

_bSaveCustomCascadeParams = true;
break;
}
}


var bDisabled = (selType.DataValue != _oCascadeTypes.sCustom);
selCascadeAssign.Disabled	= bDisabled;
selCascadeShare.Disabled	= bDisabled;
selCascadeUnshare.Disabled	= bDisabled;
selCascadeReparent.Disabled	= bDisabled;

selCascadeMerge.Disabled	= true;
selCascadeDelete.Disabled	= bDisabled || !_bCustom;
}

function onDisplayOptionChange(selDisplayOptionPar, selDisplayAreaPar, txtCustomLabelPar, txtDisplayOrderPar)
{
switch(selDisplayOptionPar.DataValue)
{
case _oDisplayOptionTypes.sDoNotDisplay:
{
selDisplayAreaPar.Disabled = true;
txtCustomLabelPar.Disabled = true;
txtDisplayOrderPar.Disabled = true;
break;
}
case _oDisplayOptionTypes.sUseCollectionName:
{
selDisplayAreaPar.Disabled = false;
txtCustomLabelPar.Disabled = true;
txtDisplayOrderPar.Disabled = false;
break;
}
case _oDisplayOptionTypes.sUseLabel:
{
selDisplayAreaPar.Disabled = false;
txtCustomLabelPar.Disabled = false;
txtDisplayOrderPar.Disabled = false;
break;
}
}
}






function isFormDirty()
{
return _sInputXml != getRelationshipXml();
}
var _schemaNamePreviousValue = null;
function createSchemaName(sEntity1, sEntity2)
{
if (_bCreate &&
(sEntity1 != null && sEntity1.length > 0) &&
(sEntity2 != null && sEntity2.length > 0)&&
(txtSchemaName.DataValue == null || txtSchemaName.DataValue.length == 0 || txtSchemaName.DataValue == _schemaNamePreviousValue))
{
txtSchemaName.DataValue = (sEntity1 + "_" +  sEntity2).substr(0, txtSchemaName.maxLength);
txtSchemaName.DataValue = txtSchemaName.DataValue.toLowerCase();
_schemaNamePreviousValue = txtSchemaName.DataValue;
}
}
var _intersectEntityNamePreviousValue = null;

function createIntersectEntityName()
{
if (_bCreate &&
(selCEName.DataValue != null && selCEName.DataValue.length > 0) &&
(selOEName.DataValue != null && selOEName.DataValue.length > 0)&&
(txtIntersectEntityName.DataValue == null || txtIntersectEntityName.DataValue.length == 0 || txtIntersectEntityName.DataValue == _intersectEntityNamePreviousValue))
{
txtIntersectEntityName.DataValue = (selCEName.DataValue + "_" +  selOEName.DataValue).substr(0, txtIntersectEntityName.maxLength);
txtIntersectEntityName.DataValue = txtIntersectEntityName.DataValue.toLowerCase();
_intersectEntityNamePreviousValue = txtIntersectEntityName.DataValue;
}
}
